const express = require('express')
const app = express()
const cors = require('cors')
const userLogin = require('./router/login')
const userInfo = require('./router/UserInfo')
const other = require('./router/other')
const {expressjwt} = require('express-jwt')
const tokenConfig = require('./tokenConfig')
// 处理跨域
app.use(cors())

app.use(express.json())
app.use(express.urlencoded({ extended: false }))
// 在挂载路由之前挂载解析token的中间件,过滤掉login和reguser路由
app.use(expressjwt({ secret: tokenConfig.secretKey, algorithms: ['HS256'] }).unless({ path: ['/login' , '/register'] }))
app.use('/', userLogin)
app.use('/', userInfo)
app.use('/', other)

// 捕获错误
app.use((err,req,res,next)=>{
    if(err.name == 'UnauthorizedError'){
        return res.send('token认证失败',err)
    }
})
app.listen(80, () => {
    console.log('server is running at http://127.0.0.1')
})